<template>
  <div class="system-user-dialog-container">
    <el-dialog :close-on-click-modal="false" :title="dataForm.userId ? $t('common.editBtn') : $t('common.addBtn')"
      draggable v-model="visible" width="600">
      <el-form :model="dataForm" label-width="90px"  ref="dataFormRef" v-loading="loading">
        <el-row :gutter="20">
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.barCode')" prop="barcode">
              <el-input clearable v-model="dataForm.barcode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.makeUpNo')" prop="billCode">
              <el-input clearable disabled v-model="dataForm.billCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.unit')" prop="unit">
              <el-input clearable disabled v-model="dataForm.unit"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.materialName')" prop="materialName">
              <el-input clearable disabled v-model="dataForm.materialName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.materialSpec')" prop="materialSpec">
              <el-input clearable disabled v-model="dataForm.materialSpec"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.warehouseCode')" prop="warehouseCode">
              <el-input clearable disabled v-model="dataForm.warehouseCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.locationCode')" prop="locationCode">
              <el-input clearable disabled v-model="dataForm.locationCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.quantity')" prop="stockQty">
              <el-input clearable disabled v-model="dataForm.stockQty"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb-6">
            <el-form-item :label="$t('makeUp.batchNumber')" prop="batchNumber">
              <el-input clearable disabled v-model="dataForm.batchNumber"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="close">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button @click="onSubmit" type="primary" icon="check" :disabled="loading">{{
            $t('common.confirmButtonText')
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="spDialog" setup>
import { useMessage } from '/@/hooks/message';
import { print, getBarCode } from '/@/api/platform/wms/makeUp';
import { ref, reactive } from 'vue';
// 通知父组件刷新
const emit = defineEmits(['refresh']);
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);

const loading = ref(false);
const size = ref<'default' | 'large' | 'small'>('default');
import { useI18n } from 'vue-i18n';
import {dicItemByCode} from "/@/api/platform/mes/screen";
import {sendPrint} from "/@/api/platform/wms/reprintbarcode";
const { t } = useI18n();

const dataForm = reactive({
    billCode: '',
    unit: '',
    materialId: '',
    materialName: '',
    materialCode: '',
    materialSpec: '',
    warehouseCode: '',
    locationCode: '',
    locationId: '',
    locationName: '',
    stockQty: null,
    batchNumber:'',
    printerName:''
});


// 打开弹窗
const openDialog = async (data: any) => {
  visible.value = true;
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });
   //获取条码号
   await getBarCode(data.reprintType).then((res) => {
    dataForm.barcode = res;
  });

  dataForm.billCode = data.billCode;
  dataForm.unit = data.unit;
  dataForm.materialId = data.materialId;
  dataForm.materialCode = data.materialCode;
  dataForm.materialName = data.materialName;
  dataForm.materialSpec = data.materialSpec;
  dataForm.warehouseCode = data.warehouseCode;
  dataForm.warehouseId = data.warehouseId;
  dataForm.warehouseName = data.warehouseName;
  dataForm.locationCode = data.locationCode;
  dataForm.locationId = data.locationId;
  dataForm.locationName = data.locationName;
  dataForm.stockQty = data.stockQty;
  dataForm.batchNumber = data.batchNumber;
  dataForm.printerName = data.printerName;
};

// 提交
const onSubmit = async () => {
  try {
    loading.value = true;

    await print(dataForm).then((r) => {
      if (r.ok) {
        dicItemByCode({ code: 'printEquipAddress' }).then((res) => {
          let url = res.data[0].value
          let data = {
            billCode:r.data.billCode,
            barCode:r.data.barcode,
            materialCode:r.data.materialCode,
            materialName:r.data.materialName,
            specification:r.data.materialSpec,
            produceDate:r.data.produceDate ? r.data.produceDate.slice(0, 10) : '',
            hostid:r.data.equipmentCode ? r.data.equipmentCode: '',
            num:r.data.checkQty,
            uuit:r.data.unit,
            printerName:dataForm.printerName,
          }
          sendPrint(url,data)

        })
      }else {
        useMessage().error(r.data);
      }
    });
    visible.value = false; // 关闭弹窗
    emit('refresh');
  } catch (error: any) {
    useMessage().error(error.msg);
  } finally {
    loading.value = false;
  }
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
